package com.bdqn.service.platformList.platform;

import bean.Platform;
import bean.PlatformMember;
import com.bdqn.common.utils.ResponseUitl;
import com.bdqn.common.utils.VerifyCodeUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class Taobaojia implements AbstractExecutor {

    JiGuangPushUtil uni_push=new JiGuangPushUtil();
    String accessToken = "";


    Map<String, List<Object>> result_map;
    //买号信息
    List<Object> result_list;


    /**
     * 获取买号
     */
    String listPlatformAccountApi = "/Mobile/BindTb.aspx";


    String loginApi = "api/user/login";

    @Override
    public Map<String, List<Object>> process(PlatformMember platformMember) {
        int status = login(platformMember);

        if(status == VerifyCodeUtil.LOGIN_ERROR){
            return new HashMap<String,List<Object>>(){
                {
                    put("登录 "+platformMember.getPlatformName()+" 失败/账号密码错误", Collections.emptyList());
                }
            };
        }
        status=getPlatformAccount(platformMember);
        if(status == VerifyCodeUtil.BUYER_ERROR){
            return new HashMap<String,List<Object>>(){
                {
                    put("获取 "+platformMember.getPlatformName()+"平台 获取买号失败/无审核的买号!",Collections.emptyList());
                }
            };
        }
        return result_map;


    }

    public int login(PlatformMember platformMember){
        String loginApi = "/mobile/login.aspx";
        platformMember.setUrl(platformMember.getUrl().substring(0,platformMember.getUrl().length()-18));
        System.out.println("尝试登录中.......");
        Map<String,String> map =new HashMap<String,String>();
        map.put("mobile",platformMember.getAccount());
        map.put("password",platformMember.getPassword());

        List<String> result = ResponseUitl.getCookieByDoPost(platformMember.getUrl()+loginApi,null ,map , "utf-8");

        System.out.println("-------cookie-----------");
        System.out.println("Cookie"+result);
        System.out.println("------------");
        System.out.println(result.get(0));
        accessToken=result.get(1);
        System.out.println("------------");
        System.out.println(accessToken);


        if("false".equals(ResponseUitl.pullMessage(result.get(0),"IsSuccess"))){
            uni_push.transmit(platformMember.getClientId(),
                    uni_push.productionMessage(platformMember.getNamePinyin(),
                            "登录"+platformMember.getPlatformName()+"平台失败/账号密码错误",VerifyCodeUtil.LOGIN_ERROR));
            return -1;
        }
        uni_push.transmit(platformMember.getClientId(),
                uni_push.productionMessage(platformMember.getNamePinyin(),
                        "成功登录"+platformMember.getPlatformName()+"平台",VerifyCodeUtil.LOGIN_SUCCESS));
        return 1;
    }

    /**
     * 自动获取买号
     * @return
     */
    public int getPlatformAccount(PlatformMember platformMember){
        Map<String,String> paramsMap =new HashMap<>();
        paramsMap.put("Cookie",accessToken);

        List<String> list=ResponseUitl.getCookieByDoPost("https://wap.sjlh123.com/mobile/MyTaskList.aspx",paramsMap,null,"utf-8");

        result_list=new ArrayList<>();

        String result=list.get(0);

        System.out.println(result);

        if(null == result || "".equals(result)){
            return -2;
        }

        Map<String,Object> value_map=new HashMap<>();
        if(result!=null && !"".equals(result)) {
            Platform platform = null;
            Document document1 = Jsoup.parse(result);
            Element postList = document1.getElementById("account_id");
            Elements postItems = postList.select("option");
            for (Element postItem : postItems) {
                String text = postItem.text();
                System.out.println(text);
                if(text.equals("全部")){
                    continue;
                }
                platform=new Platform();
                platform.setName(text);
                result_list.add(platform);
            }

        }

        uni_push.transmit(platformMember.getClientId(),uni_push.productionMessage(platformMember.getNamePinyin(),"获取到"+result_list.size()+"个买号",VerifyCodeUtil.BUYER_SUCCESS));

        result_map = new HashMap<>();
        result_map.put("获取 "+platformMember.getPlatformName()+" 平台买号成功!",result_list);
        return 1;
    }
}
